using System.Collections.Generic;
using IBatisNet.DataMapper;
using System.Collections;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.SessionStore;
using System.Configuration;

namespace Shop.Data.IBatis
{
    public class IBatisMapperManager
    {
        // Fields
        private static readonly object locker = new object();
        private static readonly IDictionary<string, ISqlMapper> mappers = new Dictionary<string, ISqlMapper>();

        // Methods
        public static ISqlMapper GetMapper(string name)
        {
            ISqlMapper mapper = null;
            if (mappers.ContainsKey(name))
            {
                return mappers[name];
            }
            string resource = name;
            Hashtable section = ConfigurationManager.GetSection("iBatisMapper") as Hashtable;
            if ((section != null) && section.ContainsKey(name))
            {
                resource = section[name].ToString();
            }
            lock (locker)
            {
                if (!mappers.ContainsKey(name))
                {
                    mapper = new DomSqlMapBuilder().Configure(resource);
                    mapper.SessionStore = new HybridWebThreadSessionStore(mapper.Id);
                    mappers.Add(name, mapper);
                    return mapper;
                }
                return mappers[name];
            }
        }
    }



}
